package com.msf.modules.sys.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.msf.common.domain.vo.Result;
import com.msf.modules.sys.domain.query.DictItemQuery;
import com.msf.modules.sys.entity.SysDictItem;
import com.msf.modules.sys.service.ISysDictItemService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.Date;


/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author author
 * @since 2024-10-09
 */
@Api(tags = "数据字典管理")
@RestController
@RequestMapping("/sys/dict/item")
@RequiredArgsConstructor
public class SysDictItemController {

    private final ISysDictItemService sysDictItemService;

    @ApiOperation("字典项列表")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public Result<IPage<SysDictItem>> queryPageList(DictItemQuery query) {
        Result<IPage<SysDictItem>> result = new Result<IPage<SysDictItem>>();
        LambdaQueryWrapper<SysDictItem> queryWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotBlank(query.getKeywords())) {
            queryWrapper.and(e -> e
                    .like(SysDictItem::getItemText, query.getKeywords())
                    .or()
                    .like(SysDictItem::getItemValue, query.getKeywords())
            );
        }
        queryWrapper.eq(StringUtils.isNotBlank(query.getDictId()),SysDictItem::getDictId, query.getDictId());
        queryWrapper.orderByAsc(SysDictItem::getSortOrder);
        Page<SysDictItem> page = new Page<>(query.getPageNo(), query.getPageSize());
        IPage<SysDictItem> pageList = sysDictItemService.page(page, queryWrapper);
        result.setSuccess(true);
        result.setResult(pageList);
        return result;
    }

    @ApiOperation("添加字典项")
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public Result<SysDictItem> add(SysDictItem sysDictItem) {
        Result<SysDictItem> result = new Result<SysDictItem>();
        try {
            SysDictItem oldItem = sysDictItemService.lambdaQuery().eq(SysDictItem::getItemValue, sysDictItem.getItemValue()).one();
            if (oldItem != null) {
                return Result.error("该值已存在！");
            }
            sysDictItem.setCreateTime(new Date());
            sysDictItem.setStatus(1);
            sysDictItemService.save(sysDictItem);
            result.success("保存成功！");
        } catch (Exception e) {
            result.error500("操作失败");
        }
        return result;
    }

    @ApiOperation("修改字典项")
    @RequestMapping(value = "/edit", method = {RequestMethod.PUT, RequestMethod.POST})
    public Result<SysDictItem> edit(SysDictItem sysDictItem) {
        Result<SysDictItem> result = new Result<SysDictItem>();
        SysDictItem sysdict = sysDictItemService.getById(sysDictItem.getId());
        if (sysdict == null) {
            result.error500("未找到对应实体");
        } else {
            sysDictItem.setUpdateTime(new Date());
            boolean ok = sysDictItemService.updateById(sysDictItem);
            if (ok) {
                result.success("编辑成功!");
            }
        }
        return result;
    }

    @ApiOperation("删除字典项")
    @RequestMapping(value = "/delete", method = RequestMethod.DELETE)
    public Result<SysDictItem> delete(@RequestParam(name = "id", required = true) String id) {
        Result<SysDictItem> result = new Result<SysDictItem>();
        SysDictItem joinSystem = sysDictItemService.getById(id);
        if (joinSystem == null) {
            result.error500("未找到对应实体");
        } else {
            boolean ok = sysDictItemService.removeById(id);
            if (ok) {
                result.success("删除成功!");
            }
        }
        return result;
    }

    @ApiOperation("批量删除字典项")
    @RequestMapping(value = "/deleteBatch", method = RequestMethod.DELETE)
    public Result<SysDictItem> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        Result<SysDictItem> result = new Result<SysDictItem>();
        if (ids == null || ids.trim().isEmpty()) {
            result.error500("参数不识别！");
        } else {
            this.sysDictItemService.removeByIds(Arrays.asList(ids.split(",")));
            result.success("删除成功!");
        }
        return result;
    }

}
